#!/usr/bin/perl
#index.cgi
#config the printers

require './backup-lib.pl';
&init_config();
&ReadParse();

&header($text{'index_title'}, "", "index", 0, 1,undef,undef);
print "<form action=save_schedule.cgi method=post>\n";
print "<table width=90% class=mode2><tr>\n";

my @vollist;
&read_file_byline("/etc/nas/voltab",\@lref);
foreach $line (@lref)
{
	next if($line =~ /^#/);
	if($line =~ /^(volume\d)\s+\S+\s+(\S+)\s+/)
	{
		if($2 eq "normal")
		{
			push @vollist,$1;
		}
	}

}
my $ROOT = &get_data_root();
print "<tr><td>$text{'volumeselect'}</td>\n";
print "<td><select name=volselect>";
foreach my $vol (@vollist)
{
	printf "<option value=$vol %s>$vol\n", ($ROOT eq $vol)?"selected":"";
}
print "</select></td></tr>\n";

#print "<td><input type=radio name=schedulartype value=FIXTIME>$text{'FIXTIME'}</b></td>","<td><input type=text name=hour size=2>:<input type=text name=minute size=2></td>";
my $configfile = "/etc/backuppc/config.pl";
my (@lref,$line,$olduser,$oldpasswd);
&read_file_byline($configfile,\@lref);
foreach $line (@lref)
{
	next if($line =~ /^#/);
        if($line =~ /Conf{SmbShareUserName}\s=\s'(.*)';$/)
        {
        	$olduser = $1;
	}
        if($line =~ /Conf{SmbSharePasswd}\s=\s'(.*)';$/)
        {
        	$oldpasswd = $1;
	}
}

print "<tr><td>$text{'NOTEFORACCOUNT'}</td>";
print "<td>$text{'user'}<input type=text name=user value=$olduser></td><td>$text{'passwd'}<input type=password name=passwd value=$oldpasswd></td></tr>";
print "<td>$text{'FIXTIME'}</b></td>","<td><input type=text name=hour size=2>:<input type=text name=minute size=2></td>";
print "<td><table><tr><td><input class=sbutton type=button name=scheaction onclick='addone();' value=\'$text{'button_add'}\'></td></tr>\n";
print "<tr><td><input class=sbutton type=button name=scheaction onclick='removeone();' value=\'$text{'button_remove'}\'></td></tr>\n";
print "</table>\n";
print "</td><td>\n";
@schedulist = &get_schedulist();
foreach $sche (@schedulist)
{
	$minute = sprintf("%02.0f",($sche*10000000)%10000000*60/10000000);
	use integer;
	$hour = sprintf("%02d",$sche*1);
	push @newschedulist,$hour.":".$minute;
}
print "<select class=list2 name=localgrouplist size=7 multiple>\n";
        $i = 0;
        while($i < @newschedulist)
        {
                chomp($newschedulist[$i]);
                print "<option value=\'$newschedulist[$i]\'>$newschedulist[$i]\n";
                $i++;
        }
print "</select>\n";
print "<input name=listinput type=hidden value=>";
print "</td></tr>";


#print "<tr><td><input type=radio name=schedulartype value=EVERYTIME>$text{'EVERYTIME'}</b></td>","<td><input type=text name=hour size=2></td></tr>";
print "<tr><td><input type=submit class=button1 name=submit value=$text{'submit'} onclick='checkselect();'></td></tr>";
print "</table><p></p>\n";

print <<EOF;
<script>
function addone()
{
	var j= document.forms[0].localgrouplist.options.length;
	hour =  document.forms[0].hour.value;
	minute =  document.forms[0].minute.value;
	if((hour == "") || (minute == "") || (hour > 24) || (hour < 0) || (minute >59) || (minute < 0))
	{
		return;
	}
	newschedule = hour+":"+minute;
	for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
        {
                if(newschedule == document.forms[0].localgrouplist.options[i].text)
		{
			return;
		}
        }

	document.forms[0].localgrouplist.options[j] = new Option(newschedule,newschedule);
	j = document.forms[0].localgrouplist.options.length;

	if(document.forms[0].localgrouplist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].localgrouplist.options[0].text;
	}
	else
        {
                document.forms[0].listinput.value = "";
        }

        for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
        {
		document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
        }
}
function removeone()
{
	for(i = document.forms[0].localgrouplist.options.length; i > 0; i--)
        {
                if(document.forms[0].localgrouplist.options[i - 1].selected)
                {
                        document.forms[0].localgrouplist.options[i - 1] = null;
                }
        }

        if(document.forms[0].localgrouplist.options.length > 0)
        {
                document.forms[0].listinput.value = document.forms[0].localgrouplist.options[0].text;
        }
        else
        {
                document.forms[0].listinput.value = "";
        }

        for(i = 1; i < document.forms[0].localgrouplist.options.length; i++)
        {
                document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
        }
        //history.go(0);

}
function checkselect()
{
        if(document.forms[0].listinput.value == "")
        {
                for(i = 0; i < document.forms[0].localgrouplist.options.length; i++)
                {
                        document.forms[0].listinput.value += " " + document.forms[0].localgrouplist.options[i].text;
                }
        }
}

</script>
EOF

&footer("/",$text{'index'});
